<?php

/**
 * Description of User
 *
 * @author fabrice
 */
class Model_User extends PDO_Mysql {

    protected $_name = 'user';
    protected $_primary = 'id_user';

    /**
     * Recupere tout les utilisateurs
     * @return <array>
     */
    public function getUserById($id) {
        $sql = 'SELECT * FROM ' . $this->_name . ' WHERE id = "' . $id ."'";
        $result = $this->pquery($sql);

        $row = $this->pfetch($result);
        return $row;
    }
    
    public function getUserByFBId($id) {
        $sql = 'SELECT * FROM `' . $this->_name . '` WHERE id_facebook = "' . $id .'"';
        $result = $this->pquery($sql);

        $row = $this->pfetch($result);
        
        return $row;
    }
    
    /**
     * Recupere tout les utilisateurs
     * @return <array>
     */
    public function getAllUser() {
        $sql = 'SELECT * FROM ' . $this->_name;
        $result = $this->pquery($sql);

        $row = $this->pfetchAll($result);
        return $row;
    }
    
    /**
     * Recupere tout les utilisateurs avec les infos
     * @return <array>
     */
    public function getAllUserWithInfo() {
        $sql = 'SELECT * FROM ' . $this->_name . " LEFT JOIN user_info ON user_info.id_user = user.id_user";
        $result = $this->pquery($sql);

        $row = $this->pfetchAll($result);
        //$row = $sql;
        return $row;
    }
    
    /**
     * Recupere tout les utilisateurs avec les infos
     * @return <array>
     */
    public function checkUserExist($uid) {
        $sql = 'SELECT COUNT(*) as C FROM ' . $this->_name . ' WHERE id_facebook = "'.$uid.'"';
        $result = $this->pquery($sql);
        $row = $this->pfetchObj($result);
        //$row = $sql;
        return $row->C;
    }

    /**
     * Insert dans la BDD
     * @param <array> $formData
     * @return <int> $row
     */
    public function setUser($formData) {
        $sql = 'INSERT INTO ' . $this->_name . ' (id_user,nom,prenom,email,id_facebook,date_creation)' .
                ' VALUES (:id_user,:nom,:prenom,:email,:id_facebook,:date_creation)';
        $data = array(
            ':id_user' => NULL,
            ':nom' => $formData['nom'],
            ':prenom' => $formData['prenom'],
            ':email' => $formData['email'],
            ':id_facebook' => $formData['id_facebook'],
            ':date_creation' => $formData['date_creation']
        );
        $row = $this->pinsert($sql, $data, 'id_user');
        return $row;
    }

}

?>